/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlinganhang.dao;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import quanlinganhang.dto.AccountTypeDTO;
import quanlinganhang.dto.UserDTO;

/**
 *
 * @author phamduong
 */
public class AccountTypeDAO {

    /**
     * Return list account
     *
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static ArrayList getAccountType() throws SQLException, ClassNotFoundException {
        return SqlDAOHelper.ExecuteQuery("GetAccountTypes", null);
    }

    /**
     * Delete 1 item from account type
     *
     * @param id
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static boolean DeleteAccountType(long id) throws SQLException, ClassNotFoundException {
        List<Parameter> sqlParams = new ArrayList<>();
        sqlParams.add(new Parameter("MaLoaiTK", id));
        return (SqlDAOHelper.ExecuteNonQuery("DeleteLoaiTK", sqlParams) > 0);
    }

    /**
     * Update 1 account type item
     *
     * @param accType
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static boolean UpdateAccountType(AccountTypeDTO accType) throws SQLException, ClassNotFoundException {
        List<Parameter> sqlParams = new ArrayList<>();
        sqlParams.add(new Parameter("MaLoaiTK", accType.getMaLoaiTK()));
        sqlParams.add(new Parameter("TenLoaiTK", accType.getTenLoaiTK()));
        sqlParams.add(new Parameter("LaiSuat", accType.getLaiSuat()));
        sqlParams.add(new Parameter("KyHan", accType.getKyHan()));
        sqlParams.add(new Parameter("GhiChu", accType.getMaLoaiTK()));

        return (SqlDAOHelper.ExecuteNonQuery("UpdateLoaiTK", sqlParams) > 0);
    }

    /**
     * Add account type
     *
     * @param accType
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static boolean AddAccountType(AccountTypeDTO accType) throws SQLException, ClassNotFoundException {
        List<Parameter> sqlParams = new ArrayList<>();
        sqlParams.add(new Parameter("MaLoaiTK", accType.getMaLoaiTK()));
        sqlParams.add(new Parameter("TenLoaiTK", accType.getTenLoaiTK()));
        sqlParams.add(new Parameter("LaiSuat", accType.getLaiSuat()));
        sqlParams.add(new Parameter("KyHan", accType.getKyHan()));
        sqlParams.add(new Parameter("GhiChu", accType.getMaLoaiTK()));

        return (SqlDAOHelper.ExecuteNonQuery("Insert_LoaiTK", sqlParams) > 0);
    }

    /**
     * Get account type name from id
     * @param id
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    public static String GetFromId(long id) 
            throws ClassNotFoundException, SQLException {
        SqlDAOHelper.OpenConnection();
        CallableStatement cs = SqlDAOHelper.connection.prepareCall("{call GetAccountTypeName (?, ?)}");
        cs.setObject("MaLoaiTK", id);
        cs.registerOutParameter("TenLoaiTK", java.sql.Types.NVARCHAR);
        cs.executeUpdate();
        String name = cs.getString("TenLoaiTK");
        SqlDAOHelper.CloseConnection();
        return name;
    }
}
